<%@ page import="fi.hell.pollit.domain.*" %>

<g:set var="loggedInUser" value="${User.getLoggedInUser()}" />

<html>
    <head>
        <title>PollIT - Make your choice</title>
        <meta name="layout" content="main" />
    </head>
    <body>
        <div id="panel">
            <g:render template="panel" model="${[loggedInUser:loggedInUser]}" />
        </div>
        <div id="pageBody">
            <h1>PollIT</h1>

            <div id="availablePollList" class="dialog">
                <g:if test="${params.show}">[<a href="${grailsApplication.config.grails.serverURL}"><g:message code="ongoingPolls" default="Ongoing polls" /></a>]</g:if>
                <g:else>[<g:message code="ongoingPolls" default="Ongoing polls" />]</g:else>
                <g:if test="${params.show != 'closed'}">[<a href="${createLink(params:['show':'closed'])}"><g:message code="closedPolls" default="Closed polls" /></a>]</g:if>
                <g:else>[<g:message code="closedPolls" default="Closed polls" />]</g:else>

                <g:if test="${flash.error}">
                    <div class="errors">${flash.error}</div>
                </g:if>

                <g:if test="${loggedInUser}">[<g:link controller="poll" action="create"><g:message code="poll.create" default="Create new poll" /></g:link>]</g:if>
                <br /><br />
                
                <g:if test="${!params.show}">
                    <g:each var="poll" in="${Poll.listAvailable()}">
                        <g:if test="${!poll.isUserVoted(loggedInUser, request.getRemoteAddr())}">
                            <g:render template="showPollForm" model="${[poll:poll, loggedInUser:loggedInUser]}" />
                        </g:if>
                        <g:else>
                            <g:render template="showDistributions" model="${[poll:poll]}" />
                        </g:else>
                    </g:each>
                 </g:if>
                <g:elseif test="${params.show == 'closed'}">
                    <g:each var="poll" in="${Poll.listClosed()}">
                        <g:render template="showDistributions" model="${[poll:poll, expired:true]}" />
                    </g:each>
                </g:elseif>
            </div>
        </div>
    </body>
</html>
